import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:ichinesereader/models/common/SelectOption.dart';
import 'package:get/get.dart';

import '../../themes/appTheme.dart';

class ButtonGroupWidget extends StatelessWidget {
  ButtonGroupWidget({
    Key? key,
    required this.name,
    required this.label,
    required this.options,
    this.initialValue,
  }) : super(key: key);

  final String name;
  final String label;
  final List<SelectOption> options;
  final String? initialValue;

  @override
  Widget build(BuildContext context) {
    return FormBuilderField(
        name: name,
        initialValue: initialValue,
        builder: (FormFieldState<dynamic> field) {
          return InputDecorator(
            decoration: InputDecoration(
              labelText: label,
              contentPadding: EdgeInsets.only(
                top: 21.0,
              ),
              border: InputBorder.none,
              errorText: field.errorText,
            ),
            child: ToggleButtons(
              color: Get.isDarkMode ? Get.theme.primaryColor : Colors.blueGrey,
              selectedColor:
                  Get.isDarkMode ? Colors.black : AppTheme.colors.lightOrange,
              fillColor:
                  Get.isDarkMode ? Colors.white : AppTheme.colors.orangeBtn,
              children: options.map((option) {
                return Container(
                    padding: EdgeInsets.fromLTRB(9, 6, 9, 6),
                    child: Text("${option.label}".tr));
              }).toList(),
              onPressed: (int index) {
                field.didChange(options[index].value);
              },
              isSelected:
                  options.map((option) => option.value == field.value).toList(),
            ),
          );
        });
  }
}
